// pages/shopMall/shopMall.js
var api = require('../../utils/api.js');
let regeneratorRuntime = require("../../utils/regenerator-runtime/runtime-module.js");
var app = getApp();
Page({
  // 设置分享到朋友圈的标题
  onShareTimeline: function () {
    return {
      title: "智克艺术 - 商城",
      query: ""
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    option1: [
      { text: '销量', value: 0 },
      { text: '销量升序', value: 1 },
      { text: '销量降序', value: 2 }
    ],
    option2: [
      { text: '价格', value: 'a' },
      { text: '价格升序', value: 'b' },
      { text: '价格降序', value: 'c' }
    ],
    value1: 0,
    value2: 'a',
    urls: [],
    goodsList: [],
    pageNum: '',
    keywords: '',
    sales: 0,
    price: 0,
    banner: [],
    noHide:true,
    // title1:'销量',
    // title2: '价格'
    array: ['销量', '销量升序', '销量降序'],
    array1: ['价格', '价格升序', '价格降序'],
    idx: 0,
    idx1: 0,
    listData:[]
  },
  bindPickerChange(e) {
    let idx = e.detail.value
    this.setData({
      idx
    })
    switch (idx) {
      case '0':    //默认销量排序
        this.setData({
          sales: 0
        })
        this.getGoodsList(1, this.data.keywords, this.data.sales, this.data.price);
        break;
      case '1':   //销量升序
        this.setData({
          sales: 2
        })
        console.log(this.data.value1)
        this.getGoodsList(1, this.data.keywords, this.data.sales, this.data.price);
        break;
      case '2':   //销量降序
        this.setData({
          sales: 1
        })
        this.getGoodsList(1, this.data.keywords, this.data.sales, this.data.price);
        break;
    }
  },
  bindPickerChange1(e) {
    let idx1 = e.detail.value
    this.setData({
      idx1
    })
    switch (idx1) {
      case '0':   //价格默认排序
        this.setData({
          price: 0
        })
        this.getGoodsList(1, this.data.keywords, this.data.sales, this.data.price);
        break;
      case '1':   //价格升序
        this.setData({
          price: 2
        })
        this.getGoodsList(1, this.data.keywords, this.data.sales, this.data.price);
        break;
      case '2':   //价格降序
        this.setData({
          price: 1
        })
        this.getGoodsList(1, this.data.keywords, this.data.sales, this.data.price);
        break;
    }
  },

  //上新
  shangXin() {
    this.setData({
      idx: 0,
      idx1: 0
    })

    this.getGoodsList(1, this.data.keywords, 0, 0);
  },
  //banner图跳店铺
  toDetail(e) {
    let id = e.currentTarget.dataset.id;
    console.log(id)
    // wx.navigateTo({
    //   url: '/pages/productDetail/productDetail?id='+id,
    // })
    wx.navigateTo({
      url: '/pages/shopList/shopList?store_id=' + id
    })
  },

  //搜索
  searchGoods(e) {
    var keywords = e.detail.value
    this.setData({
      keywords
    })

  },
  search1() {
    this.getGoodsList(1, this.data.keywords, this.data.sales, this.data.price);
  },

  //跳转商品详情
  productDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/productDetail/productDetail?id=' + id,
    })
  },

  //跳转店铺商品列表
  toShopList(e) {
    let store_id = e.currentTarget.dataset.sid;
    console.log(store_id)
    wx.navigateTo({
      url: '/pages/shopList/shopList?store_id=' + store_id,
    })
  },
   // 获取全部分销品牌
   async getAllData() {
    let token = wx.getStorageSync('token')
    let res = await api.fetchApi('index/store_lis', { token: token })
    if(res.code = 2000) {
      this.setData({
        listData: res.data
      })
    }else {
      if(res.data==''){
        wx.showToast({
          title: '请先登录',
          icon:'none'
        })
        wx.switchTab({
          url: '/pages/personal/personal',
        })
      }
      return
    }
  },
  //获取banner
  getBanners() {
    api.fetchApi('/getBanners').then(res => {
      let urls = res;
      this.setData({
        urls
      })
    })
  },
  // 控制导航栏显示隐藏
  hideNavbar(){
    const that = this
    wx.request({
      url: app.globalData.host+'test/index_a',
      method:'GET',
      success(res){
        that.setData({
          noHide:res.data.data==0?false:true
        })
      },
      fail(err){
        console.log(err)
      }
    })
  },
  // 跳转品牌商品列表
  goBrand(e){
    let store_id = e.currentTarget.dataset.id;
    console.log(store_id)
    wx.navigateTo({
      url: '/pages/shopList/shopList?store_id=' + store_id,
    })
  },
  checktoken(){
    let token = wx.getStorageSync('token')
    if(token==''){
      wx.switchTab({
        url: '/pages/personal/personal',
      })
      return
    }
  },
  //获取商城首页商品列表
  getGoodsList(page, keywords, sales, price) {
    let params = {
      page,
      keywords,
      sales,
      price,
      token: wx.getStorageSync('token')
    }
    api.fetchApi('/Goods/index_a', params).then(res => {
      wx.stopPullDownRefresh()
      let banner = res.banner;
      var goodsList = this.data.goodsList;
      var goodsList1 = res.list;
      if (page == 1) {
        goodsList = res.list;
      } else {
        goodsList = [...goodsList, ...goodsList1]
      }
      this.setData({
        banner,
        goodsList,
        page,
        pageNum: goodsList1.length
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.checktoken()
    this.hideNavbar()
    this.getAllData()
    this.getGoodsList(1, '', 0, 0);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.checktoken()
    this.hideNavbar()
    this.getAllData()
    this.getGoodsList(1, '', 0, 0);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getGoodsList(1);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // this.checktoken()
    var page = this.data.page || 1;
    page++;
    if (this.data.pageNum < 10) {
      wx.showToast({
        title: '暂无更多',
        icon: 'none'
      })
      return
    }
    this.getGoodsList(page, this.data.keywords, this.data.sales, this.data.price);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})